﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using DataBasetestApp.ServiceReference2;

namespace DataBasetestApp
{
    public partial class LoginPage : PhoneApplicationPage
    {
        // Set up variables and isolated storage
        bool loginSuccessful = false;
        string username, password;
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;

        public LoginPage()
        {
            InitializeComponent();
            InitializeSettings();
            checkBox1.IsChecked = true; 
        }

        private void InitializeSettings()
        {
            if (settings.Contains("Username"))
            {
                emailBox.Text = (string)settings["Username"];
                passwordBox.Password = (string)settings["Password"];
            }
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            // Check login details against database.
            LoadingBlock.Visibility = Visibility.Visible;
            this.progressBar1.IsIndeterminate = true;
            username = emailBox.Text;
            password = passwordBox.Password;
            GAALiveServiceClient client = new GAALiveServiceClient();
            client.LoginCheckCompleted +=
                new EventHandler<LoginCheckCompletedEventArgs>(client_LoginCheckCompleted);
            client.LoginCheckAsync(username, password);
        }

        void client_LoginCheckCompleted(object sender, LoginCheckCompletedEventArgs l)
        {
            loginSuccessful = l.Result;

            if (loginSuccessful)
            {
                if (Convert.ToBoolean(checkBox1.IsChecked))
                {
                    settings["Username"] = username;
                    settings["Password"] = password;
                }
                if (!(Convert.ToBoolean(checkBox1.IsChecked)))
                {
                    settings["Username"] = "";
                    settings["Password"] = "";
                }
                LoadingBlock.Visibility = Visibility.Collapsed;
                this.progressBar1.IsIndeterminate = false;
                MessageBox.Show("Username: " + emailBox.Text + Environment.NewLine + "Login Successful");
                NavigationService.Navigate(new Uri("/Views/MatchSetupPage.xaml", UriKind.Relative));
                //NavigationService.GoBack();
            }
            else
            {
                // Loading Bar dissappears and user is notified that they cannot log in
                this.progressBar1.IsIndeterminate = false;
                LoadingBlock.Visibility = Visibility.Collapsed;
                MessageBox.Show("Incorrect username or password, please try again." + Environment.NewLine + "Are you sure you are registered?" + Environment.NewLine + "If not, please click 'Register' To create an account.");
            }
        }

        private void registerButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/RegistrationPage.xaml", UriKind.Relative));
        }

        private void checkBox1_Checked(object sender, RoutedEventArgs e)
        {
            //
        }
    }
}